package com.fafi.selfmvc.controller;

import com.fafi.selfmvc.convertor.NumberEditor;
import com.fafi.selfmvc.vo.Goods;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/goods")
@SessionAttributes("goodsList") //将model中的添加到session中
public class GoodsController {
	//得到一个用来记录日志的对象，这样在打印信息的时候能够标记打印的是哪个类的信息
	private static final Log logger = LogFactory.getLog(GoodsController.class);

	//注解验证器相当于“GoodsValidator validator = new GoodsValidator();”
	@Resource
	private Validator validator;

	/**
	 * 此配置的优先级最高
	 * (1) 查询通过@InitBindingInitializer装配的自定义编辑器
	 * (2) 查询通过ConversionService装配的自定义转换器
	 * (3) 查询通过WebBindingInitializer接口装配的全局自定义编辑器
	 */
	@InitBinder
	public void InitBinder(WebDataBinder binder) {
		logger.info("调用@InitBinder！");
		binder.registerCustomEditor(BigDecimal.class, new NumberEditor());
	}
	/*@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}*/

	@RequestMapping("/input")
	public String input(Model model){
		//如果model中没有goods属性，addGoods.jsp会抛出异常
		//因为表单标签无法找到modelAttribute属性指定的from backing object
		model.addAttribute("goods",new Goods());
		return "addGoods";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/save")
	public String save(@ModelAttribute Goods goods, BindingResult result, Model model){
		this.validator.validate(goods, result);//添加验证
		if(result.hasErrors()){
			return "addGoods";
		}
		//goodsService.save(goods);
		//执行service...


		List<Goods> list = (List<Goods>) model.getAttribute("goodsList");
		if (list != null) {
			list.add(goods);
		} else {
			try {
				List<Goods> goodsList = getGoods();
				goodsList.add(goods);
				model.addAttribute("goodsList",goodsList);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		logger.info("添加成功");

		return "goodsList";
	}

	private List<Goods> getGoods() throws ParseException {
		List<Goods> list = new ArrayList<Goods>();
		Goods goods = new Goods();
		goods.setGdate(DateFormat.getDateInstance().parse("2020-12-25"));
		goods.setGdescription("苹果");
		goods.setGname("apple");
		goods.setGprice(11.23);
		list.add(goods);

		goods = new Goods();
		goods.setGdate(DateFormat.getDateInstance().parse("2020-12-24"));
		goods.setGdescription("梨");
		goods.setGname("pears");
		goods.setGprice(10.23);
		list.add(goods);
		return list;
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(DateFormat.getDateInstance().parse("2020-12-24"));
	}
}

